package com.webinsky.folder.persist.dao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import com.webinsky.folder.persist.entity.FolderCatalog;

public interface FolderCatalogMapper {
	@Insert({
		"insert into catalog (name, item_id, sort, del, create_at, update_at, language, site_id)",
		"values (#{name,jdbcType=VARCHAR}, #{itemId, jdbcType=INTEGER}, #{sort,jdbcType=INTEGER}, #{del,jdbcType=INTEGER}, now(), now(), #{language,jdbcType=VARCHAR}, #{siteId, jdbcType=INTEGER})"
	})
	@SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
	int insert(FolderCatalog record);
	    
	@Select({
	    "select",
	    "id, name, item_id, sort, del, create_at, update_at, language, site_id",
	    "from catalog"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
	    @Result(column="item_id", property="itemId", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER)
	})
	List<FolderCatalog> selectAll();
	
	@Select({
	    "select",
	    "id, name, item_id, sort, del, create_at, update_at, language, site_id",
	    "from catalog",
	    "where id = #{id, jdbcType=INTEGER}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
	    @Result(column="item_id", property="itemId", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER)
	})
	FolderCatalog selectById(@Param("id")int id);
	
	@Select({
		"select",
	    "id, name, item_id, sort, del, create_at, update_at, language, site_id",
	    "from catalog ",
	    "where id = #{id, jdbcType=INTEGER} and del = #{del, jdbcType=INTEGER}"
	})
	@Results({
		@Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="item_id", property="itemId", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER)
	})
	List<FolderCatalog> selectByIdAndDel(@Param("id")int articleId, @Param("del")int del);
	
	@Select({
		"select",
	    "id, name, item_id, sort, del, create_at, update_at, language, site_id",
	    "from catalog ",
	    "where site_id = #{siteId, jdbcType=INTEGER} and del = #{del, jdbcType=INTEGER}"
	})
	@Results({
		@Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
	    @Result(column="item_id", property="itemId", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER)
	})
	List<FolderCatalog> selectBySiteIdAndDel(@Param("siteId")int siteId, @Param("del")int del);
	
	@SelectProvider(type = FolderCatalogSqlProvider.class, method = "selectByIds")
	@Results({
		@Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
	    @Result(column="item_id", property="itemId", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER)
	})
	List<FolderCatalog> selectByIds(@Param("ids")List<Integer> ids);	
	
	@SelectProvider( type = FolderCatalogSqlProvider.class, method = "selectByNameAndDel")
	@Results({
		@Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
	    @Result(column="item_id", property="itemId", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER)
	})
	List<FolderCatalog> selectByNameAndDel(@Param("name")String name, @Param("del")Integer del, @Param("language")String lang, @Param("pageSize")Integer pageSize, @Param("pageStart")Integer pageStart);
	
	@SelectProvider( type = FolderCatalogSqlProvider.class, method = "selectCountByNameAndDel")
	int selectCountByNameAndDel(@Param("name")String name, @Param("del")int del, @Param("language")String lang);	
	
	@SelectProvider( type = FolderCatalogSqlProvider.class, method = "selectBySiteIdNameAndDel")
	@Results({
		@Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
	    @Result(column="item_id", property="itemId", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER)
	})
	List<FolderCatalog> selectBySiteIdNameAndDel(@Param("name")String name, @Param("del")Integer del, @Param("language")String lang, @Param("siteId") int siteId, @Param("pageSize")Integer pageSize, @Param("pageStart")Integer pageStart);
	
	@SelectProvider( type = FolderCatalogSqlProvider.class, method = "selectCountBySiteIdNameAndDel")
	int selectCountBySiteIdNameAndDel(@Param("name")String name, @Param("del")int del, @Param("language")String lang, @Param("siteId") int siteId);	
	
	@SelectProvider( type = FolderCatalogSqlProvider.class, method = "selectByItemsLangAndDel")
	@Results({
		@Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
	    @Result(column="item_id", property="itemId", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER)
	})
	List<FolderCatalog> selectByItemsLangAndDel(@Param("itemIds")List<Integer> itemId, @Param("lang")String lang, @Param("del")int del, @Param("siteId")int siteId);
	
	
	@Select({
		"select",
	    "id, name, item_id, sort, del, create_at, update_at, language, site_id",
	    "from catalog ",
	    "where site_id = #{siteId, jdbcType=INTEGER} and del = #{del, jdbcType=INTEGER} and item_id = #{itemId, jdbcType=INTEGER} and language = #{lang, jdbcType=VARCHAR}"
	})
	List<FolderCatalog> selectByItemIdLangAndDel(@Param("itemId")int itemId, @Param("lang")String lang, @Param("del")int del, @Param("siteId")int siteId);
	
	@Select({
		"select",
	    "id, name, item_id, sort, del, create_at, update_at, language, site_id",
	    "from catalog ",
	    "where site_id = #{siteId, jdbcType=INTEGER} and del = #{del, jdbcType=INTEGER} and language = #{lang, jdbcType=VARCHAR}"
	})
	@Results({
		@Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
	    @Result(column="item_id", property="itemId", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER)
	})	
	List<FolderCatalog> selectBySiteLangAndDel(@Param("siteId")int siteId, @Param("lang")String lang, @Param("del")int del);
	
	@Update({
       "update catalog",
	       "set name = #{name,jdbcType=VARCHAR},",
	       "item_id = #{itemId, jdbcType=INTEGER},",
	       "sort = #{sort,jdbcType=INTEGER},",
	       "del = #{del,jdbcType=INTEGER},",
	       "create_at = #{createAt,jdbcType=TIMESTAMP},",
	       "update_at = #{updateAt, jdbcType=TIMESTAMP},",
	       "language = #{language,jdbcType=VARCHAR},",
	       "site_id = #{siteId,jdbcType=INTEGER}",
       "where Id = #{id,jdbcType=INTEGER}"
   })
   int updateByPrimaryKey(FolderCatalog record);	
}
